﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Soapi.JS Paged Results Demo</title>

    <script src="scripts/Soapi.js" type="text/javascript"></script>

    <script type="text/javascript">

        function run()
        {

            // lets page through all users with reputation at or above an arbtrary threshold
            var repFloor = 100; // 

            var apiKey = 'qgAq_KfDu0KYzlNG-qaTuw';
            var targetSite = "api.stackapps.com";


            var factory = new Soapi.RouteFactory(targetSite, apiKey);

            var usersRoute = factory.Users();
            // leverage intellisense for field discovery
            usersRoute.page = 1;
            usersRoute.pagesize = 100;
            usersRoute.sort = Soapi.Domain.UserSort.reputation;

            // or go old school and apply the parameters as a map.

            //    usersRoute = factory
            //                .Users()
            //                .applyParameters(
            //                {
            //                    page: 1,
            //                    pagesize: 100,
            //                    sort: 'reputation'
            //                });


            usersRoute.getPagedResponse(function success(data)
            {

                // output the data
                for (var idx in data.items)
                {
                    user = data.items[idx];
                    if (user.reputation >= repFloor)
                    {
                        var output = document.getElementById("output");
                        var item = document.createElement("div");

                        item.innerHTML = "<img src='http://www.gravatar.com/avatar/" + user.email_hash
                            + "?s=16' width='16' height='16'/> " + user.reputation + " " + user.display_name;

                        output.appendChild(item);

                    }
                }

            }, function failure(data)
            {
                alert(usersRoute._lastUrl + "\r\n\r\ncode: " + data.code + "\r\nmessage: " + data.message);
            }, function pageCallback(data)
            {
                // use the pageCallback to cancel the paging operation or report progress

                document.getElementById("output").innerHTML = "fetched page " + data.page;

                // rep threshold reached or no more pages, cancel the paging operation
                return data.items.length == 0 || data.items[0].reputation < repFloor;
            });
        }
    </script>

</head>
<body onload="run()">
    <div id="output">
    </div>
</body>
</html>
